//app.js
const APP_ID = 'wx582499400d94f8bb'; //输入小程序appid  
const APP_SECRET = '52f712132f9b6eae7937491516e29736'; //输入小程序app_secret  
var OPEN_ID = '' //储存获取到openid  
var SESSION_KEY = '' //储存获取到session_key  
App({

  onLaunch: function() {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    var that = this;
    // 登录
    wx.login({
      success: res => {
        var that1 = that
        wx.request({
          url: that1.globalData.baseUrl + '/safe/user/checkWxUserExist',
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          data: {
            code: res.code
          },
          method: 'POST',
          success: res => {
            console.log(res)
            if (!res.data.success) {
              wx.navigateTo({
                url: '/pages/danger/login/login',
              })
            } else {
              if (res && res.header && res.header['Set-Cookie']) {
                wx.setStorageSync('sessionid', res.header['Set-Cookie']); //保存Cookie到Storage
              }
              that1.globalData.userInfo = res.data.data
            }
          }

        })
        // // 发送 res.code 到后台换取 openId, sessionKey, unionId
        // wx.request({
        //   url: 'https://api.weixin.qq.com/sns/jscode2session',
        //   data: {
        //     appid: APP_ID,
        //     secret: APP_SECRET,
        //     js_code: res.code,
        //     grant_type: 'authorization_code'
        //   },
        //   method: 'GET',
        //   success: function(res) {
        //     console.log(res.data)
        //     OPEN_ID = res.data.openid;
        //     SESSION_KEY = res.data.session_key;
        //     console.log(OPEN_ID.length)
        //     console.log(SESSION_KEY.length)
        //     that1.globalData.openid = res.data.openid
        //     that1.globalData.session_key = res.data.session_key
           
        //   }
        // })

      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },

  globalData: {
    userInfo: null,
    sessionId: '',
    senssion_key: '',
    openId: '', //用户在该小程序上的唯一id
    // baseUrl: 'http://127.0.0.1:8080',
    // baseUrl: 'https://www.safe360.vip',
    baseUrl: 'http://106.13.33.134:8443',
    fileUrl:'http://nbjtjcsafe360.oss-cn-beijing.aliyuncs.com/',
    user: {},
    dangerInspectStatus:{
      un_inspect:2,//待检查
      un_rectify:4//待整改
    }
  }
})